// https://alasky.cds.unistra.fr/DSS/DSSColor/Norder0/Dir0/Npix0.jpg
// https://alasky.cds.unistra.fr/DSS/DSSColor/Norder9/
const jpeg = require('jpeg-js');
const https = require("https");
const fs = require("fs");
const healpix = require('@hscmap/healpix');
//theta lat; phi lon; 05h 40m 59.0s.  −02° 27′ 30.0
/*
	18h 03m 37s[1]
	−24° 23′ 12″
*/
let theta = Math.PI / 2 + (24 + 23 / 60 + 12 / 3600) * Math.PI / 180;
let phi =  15 * (18 + 3 / 60 + 37 / 3600) * Math.PI / 180;
// let theta = Math.PI / 2 + (2 + 27 / 60 + 30 / 3600) * Math.PI / 180;
// let phi =  15 * (5 + 40 / 60 + 59 / 3600) * Math.PI / 180;
let order = 4;
let nside = healpix.order2nside(order);
console.log(healpix.nside2npix(nside));
let ipix = healpix.ang2pix_nest(nside, theta, phi);
let obj = healpix.pix2ang_ring(nside, ipix);
console.log(theta, phi);
// console.log(obj);
let corners = healpix.corners_nest(nside, ipix);
let center = { theta: 0, phi: 0 };
corners.map(corner =>
{
    let obj = healpix.vec2ang(corner);
    console.log(obj)
    center.theta += obj.theta / 4;
    center.phi += obj.phi / 4;
});

healpix.query_disc_inclusive_nest(nside, healpix.ang2vec(theta, phi), 0.04, (ipix) =>
{
    console.log(ipix);
});
let resol = healpix.nside2resol(nside);
let img = new Array(1024 * 1024 * 4);
async function doImage ()
{
    await parseImage(img, await addImage(theta, phi), 0, 512);
    await parseImage(img, await addImage(theta, center.phi < phi ? phi + resol : (phi - resol)), 512, 0);
    await parseImage(img, await addImage(center.theta < theta ? (theta + resol) : (theta - resol), phi), 512, 512);
    await parseImage(img, await addImage(center.theta < theta ? (theta + resol) : (theta - resol), center.phi < phi ? (phi + resol) : (phi - resol)), 0, 0);
    let data = jpeg.encode({ data: Buffer.from(img), width: 1024, height: 1024 });
    fs.writeFileSync("output.jpg", data.data);
}
doImage();
// console.log(healpix.ang2pix_nest(nside, Math.PI / 2, Math.PI / 2));
// console.log(healpix.ring2fxy(nside, 4));
// console.log(healpix.ang2vec(0, Math.PI / 2));
// console.log(healpix.ring2fxy(8,1))
function addImage (theta, phi)
{
    return new Promise(resolve =>
    {
        let ipix = healpix.ang2pix_nest(nside, theta, phi);
        let fileUrl = (`https://alasky.cds.unistra.fr/DSS/DSSColor/Norder${order}/Dir${Math.floor(ipix / 10000) * 10000}/Npix${ipix}.jpg`);
        let filePath = `Npix${ipix}.jpg`;
        const file = fs.createWriteStream(filePath);
        https.get(fileUrl, (res) =>
        {
            res.pipe(file);
        }).end();
        file.on('finish', () => resolve(filePath));
    })
}
function parseImage (image, filename, offsetX, offsetY)
{
    return new Promise(resolve =>
    {
        console.log(filename);
        let data = jpeg.decode(fs.readFileSync(filename));
        for (let i = 0; i < data.width; i++)
        {
            for (let j = 0; j < data.height; j++)
            {
                let index = (i + offsetX) * 4 + (j + offsetY) * 1024 * 4;
                let index2 = (i + j * data.width) * 4;
                image[index] = data.data[index2];
                image[index + 1] = data.data[index2 + 1];
                image[index + 2] = data.data[index2 + 2];
                image[index + 3] = 0xff;
            }
        }
        resolve();
    })
}
function draw(canvas, theta, phi) {
    const nstep = 8
    const nside = 4
    const npix = 12 * nside * nside
    for (let ipix = 0; ipix < npix; ++ipix) {
        {
            for (let i = 0; i < nstep; ++i) {
                const ne = i / nstep
                const v = healpix.pixcoord2vec_nest(nside, ipix, ne, 0)
                console.log(v)
            }
            for (let i = 0; i <= nstep; ++i) {
                const nw = i / nstep
                const v = healpix.pixcoord2vec_nest(nside, ipix, 1, nw)
                console.log(v)
            }
        }
    }
}
// draw();